﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using Contracts;

using Model;
using System.Data;
using System.ServiceModel;
namespace Presentation
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                bindTeacherName();
                bindCourseLevel();
                using (ChannelFactory<IProjectBService> factory = new ChannelFactory<IProjectBService>("service"))
                {
                    IProjectBService service = factory.CreateChannel();
                    this.StudentListBox.DataSource = service.getStudentList(courseID);
                    this.StudentListBox.DataTextField = "StudentName";
                    this.StudentListBox.DataValueField = "StudentId";
                    this.StudentListBox.DataBind();
                    this.SelectedListBox.DataSource = service.getSelectedList(courseID);
                    this.SelectedListBox.DataTextField = "StudentName";
                    this.SelectedListBox.DataValueField = "StudentId";
                    this.SelectedListBox.DataBind();

                    if (courseID > 0)
                    {
                        List<Model.Course> Course = service.obtainCourseName(courseID);
                        this.CourseNameTextBox.Text = Course.First().CourseName;
                    }
                }
            }

        }

        /// <summary>
        /// define CourseId
        /// </summary>
        private int courseID = -1;

        private void bindCourseLevel()
        {
            using (ChannelFactory<IProjectBService> factory = new ChannelFactory<IProjectBService>("service"))
            {
                IProjectBService service = factory.CreateChannel();
                this.CourseLevelDropDownList.DataSource = service.getLevelList();
                this.CourseLevelDropDownList.DataTextField = "LevelName";
                this.CourseLevelDropDownList.DataValueField = "LevelId";
                this.CourseLevelDropDownList.DataBind();
                this.CourseLevelDropDownList.Items.Insert(0, "Please Select");
                this.CourseLevelDropDownList.SelectedIndex = 0;
            }
        }
        private void bindTeacherName()
        {
            using (ChannelFactory<IProjectBService> factory = new ChannelFactory<IProjectBService>("service"))
            {
                IProjectBService service = factory.CreateChannel();
                this.TeacherDropDownList.DataSource = service.getTeacherList();
                this.TeacherDropDownList.DataTextField = "TeacherName";
                this.TeacherDropDownList.DataValueField = "TeacherId";
                this.TeacherDropDownList.DataBind();
                this.TeacherDropDownList.Items.Insert(0, "Please Select");
                this.TeacherDropDownList.SelectedIndex = 0;
            }
        }

        protected void add_Click(object sender, EventArgs e)
        {
            if (this.StudentListBox != null && this.StudentListBox.SelectedItem != null)
            {
                this.SelectedListBox.Items.Add(this.StudentListBox.SelectedItem);
                this.StudentListBox.Items.Remove(this.SelectedListBox.SelectedItem);
                this.SelectedListBox.SelectedItem.Selected = false;
            }
        }

        protected void Remove_Click(object sender, EventArgs e)
        {
            if (this.SelectedListBox != null && this.SelectedListBox.SelectedItem != null)
            {
                this.StudentListBox.Items.Add(this.SelectedListBox.SelectedItem);
                this.SelectedListBox.Items.Remove(this.StudentListBox.SelectedItem);
                this.StudentListBox.SelectedItem.Selected = false;
            }
        }
        /// <summary>
        /// save operation  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SaveCourse_Click(object sender, EventArgs e)
        {
            using (ChannelFactory<IProjectBService> factory = new ChannelFactory<IProjectBService>("service"))
            {
                IProjectBService service = factory.CreateChannel();
                if (courseID < 0)
                {
                    SavePage(courseID);

                }

                else
                {
                    service.removeCourseDetails(courseID);
                    SavePage(courseID);
                }
            }
        }
        public void SavePage(int courseID)
        {
            using (ChannelFactory<IProjectBService> factory = new ChannelFactory<IProjectBService>("service"))
            {
                IProjectBService service = factory.CreateChannel();
                if (CourseLevelDropDownList.SelectedIndex > 0 && TeacherDropDownList.SelectedIndex > 0)
                {
                    int courseId;

                    Course course = new Course();
                    course.CourseName = CourseNameTextBox.Text.Trim();
                    course.TeacherId = int.Parse(TeacherDropDownList.SelectedValue.ToString());
                    course.LevelId = int.Parse(CourseLevelDropDownList.SelectedValue.ToString());
                    course.CourseStatus = 1;
                    if (courseID < 0)
                    {
                        courseId = service.addCourse(course);
                    }
                    else
                    {
                        course.CourseId = courseID;
                        service.updateCourse(course);
                        courseId = courseID;
                    }
                    for (int i = 0; i < SelectedListBox.Items.Count; i++)
                    {
                        CourseDetails courseDetail = new CourseDetails();
                        courseDetail.CourseId = courseId;
                        courseDetail.StudentId = int.Parse(SelectedListBox.Items[i].Value.ToString());
                        service.addCourseDetails(courseDetail);
                    }
                    if (courseID < 0)
                    {
                        Response.Write("save successfully");
                    }
                    else
                    {
                        Response.Write("update successfully");
                    }
                }
                else
                {

                    Response.Redirect("SaveCourse_UI.aspx");

                }
            }
        }
    }
}



